/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sabayframework.channel.impl;

import org.sabayframework.channel.Lock;

/**
 * Write-only channel that consumes all output.
 * @author Alex Antonau
 * */
public final class NullChannel<T> extends WriteOnlyChannel<T> {

	private static final NullChannel<Object> instance = new NullChannel<Object>();
	
	@SuppressWarnings("unchecked")
	public static <T> NullChannel<T> getInstance(){
		return (NullChannel<T>) instance;
	}
	private static class WriteLock implements Lock<Object>{
		int locked;
		public WriteLock(int size) {
			locked = size;
		}

		@Override
		public int size() {
			return locked;
		}

		@Override
		public Object get(int index) {
			return null;
		}

		@Override
		public void set(int index, Object elem) {
		}

		@Override
		public Object[] toArray() {
			return new Object[locked];
		}

		@Override
		public void unlock() {
			locked = -1;
		}

		@Override
		public void cancel() {
			locked = -1;
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Lock<T> getWriteLock(int size) {
		return (Lock<T>) new WriteLock(size);
	}

	@Override
	public void add(T elem) throws IllegalStateException {
	}

	@Override
	public boolean isEmpty() {
		return true;
	}

	@Override
	public boolean isFull() {
		return false;
	}

	@Override
	public int size() {
		return 0;
	}

	@Override
	public int capacity() {
		return Integer.MAX_VALUE;
	}

	@Override
	public int freeSize() {
		return Integer.MAX_VALUE;
	}

	@Override
	public void close() {
	}

	@Override
	public boolean isClosed() {
		return false;
	}

	@Override
	public void fail(Throwable cause) {
	}

	@Override
	public boolean hasFailed() {
		return false;
	}

	@Override
	public Throwable getFailureCause() {
		return null;
	}

}
